//
//  DHRadioViewController.m
//  Moment
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 谭泽源. All rights reserved.
//

#import "DHRadioViewController.h"
#import "DHHeadTableViewCell.h"

@interface DHRadioViewController ()<UITableViewDataSource,UITableViewDelegate,hotRadioTableCellDelegate,UIScrollViewDelegate,DHHeaderViewCellDelegate>
@property (nonatomic, retain)NSMutableArray *hotListArr;
@property (nonatomic, retain)NSMutableArray *allListArr;
@property (nonatomic, retain)NSMutableArray *carouselArr;
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)UIScrollView *headScrollView;
@property (nonatomic, retain)UIPageControl *pageC;

@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, assign)BOOL isUpLoad;
@property (nonatomic, assign)NSInteger nextPage;

@property (nonatomic, retain)NSMutableArray *refreshPicArr;

@end

@implementation DHRadioViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"电台";
    
    _allListArr = [NSMutableArray array];
    _hotListArr = [NSMutableArray array];
    _carouselArr = [NSMutableArray array];
    _refreshPicArr = [NSMutableArray array];
    
    [self createViews];

    [self addHeaderRefresh];
    [self addFooterRefresh];
    _isUpLoad = NO;
    // Do any additional setup after loading the view.
}


- (void)createViews{
    _myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    self.view.backgroundColor = [UIColor redColor];
    [_myTableView registerClass:[DHHotRadioTableViewCell class] forCellReuseIdentifier:@"hotRadio"];
    [_myTableView registerClass:[DHAllRadioTableViewCell class] forCellReuseIdentifier:@"allRadio"];
    [_myTableView registerClass:[DHHeadTableViewCell class] forCellReuseIdentifier:@"DHHeadTableViewCell"];
    [self.view addSubview:_myTableView];
    for (int i = 0; i < 29; i++) {
        NSString *imageName = [NSString stringWithFormat:@"refresh%d.png",i];
        UIImage *image = [UIImage imageNamed:imageName];
        CGSize size = CGSizeMake(20, 20);
    UIImage *newImage = [SelfAdaption image:image byScalingToSize:size];
        [_refreshPicArr addObject:newImage];
    }
}


#pragma mark -- 设置下拉刷新
-(void)addHeaderRefresh
{
    
        //当下拉时候会触发该方法
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉");
        //        self.nextPage = 0;// 记录页码的
        self.isUpLoad = NO;//标记为下拉操作
        self.nextPage = 0;
        // 重新请求数据
        [self getData];
    }];
    
    // 进到应用自动刷新(可以放在viewdidload)
    [self.myTableView.mj_header beginRefreshing];
}

#pragma mark -- 设置上拉刷新
-(void)addFooterRefresh
{
//    设置上拉刷新动画
    MJRefreshAutoGifFooter  *footer = [MJRefreshAutoGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(refreshAction)];
    [footer setImages:_refreshPicArr forState:MJRefreshStateRefreshing];
    footer.refreshingTitleHidden = YES;
    footer.stateLabel.hidden = YES;
    
    self.myTableView.mj_footer = footer;
    [self.myTableView.mj_footer beginRefreshing];
}
-(void)refreshAction{
    NSLog(@"上拉");
    self.nextPage += 9;
    self.isUpLoad = YES;
    // 重新请求数据
    [self getAFNetWorkingDataIsUpLoad];
}




-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (_carouselArr.count != 0) {
            return 1;
        }
        return 0;
    }else if (section == 1) {
        if (_hotListArr.count != 0) {
             return 1;
        }
        return 0;
    }else{
    return _allListArr.count;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *hotRadio = @"hotRadio";
    static NSString *allRadio = @"allRadio";
    static NSString *headView = @"DHHeadTableViewCell";
    DHAllRadioTableViewCell *allRadioCell = [tableView dequeueReusableCellWithIdentifier:allRadio];
    DHHotRadioTableViewCell *hotRadioCell = [tableView dequeueReusableCellWithIdentifier:hotRadio];
    DHHeadTableViewCell *headViewCell = [tableView dequeueReusableCellWithIdentifier:headView];
    allRadioCell.selectionStyle = UITableViewCellAccessoryNone;
    hotRadioCell.selectionStyle = UITableViewCellAccessoryNone;
    if (indexPath.section == 0) {
        headViewCell.picArray = _carouselArr;
        headViewCell.deleage = self;
        return headViewCell;
    }else if (indexPath.section == 1) {
        hotRadioCell.hotRadioArr = [NSMutableArray arrayWithArray:_hotListArr];
         hotRadioCell.delegate = self;
        return hotRadioCell;
    }
    else{
        allRadioCell.radio = _allListArr[indexPath.row];
        return allRadioCell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
    DHRadio *radio = [[DHRadio alloc]init];
    radio = _allListArr[indexPath.row];
    DHDetailRadioViewController *detailRadioVC = [[DHDetailRadioViewController alloc]init];
    detailRadioVC.radio = radio;
    [self.navigationController pushViewController:detailRadioVC animated:YES];
    }
}
//代理push到hotRadio详情
-(void)pushToNextViewController:(DHRadio *)radio{
    DHDetailRadioViewController *detailRadioVC = [[DHDetailRadioViewController alloc]init];
    detailRadioVC.radio = radio;
    [self.navigationController pushViewController:detailRadioVC animated:YES];
}
//headViewCell的代理
-(void)pushToRadioVC:(DHCarousel *)carousel{
    DHDetailRadioViewController *detailRadioVC = [[DHDetailRadioViewController alloc]init];

    detailRadioVC.carousel = carousel;
    [self.navigationController pushViewController:detailRadioVC animated:YES];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 180 * HEIGHT;
    }else if (indexPath.section == 1) {
        CGFloat height = (SCREEN_WIDTH - 6)/3;
        return height;
    }else{
        return 100 * HEIGHT;
    }

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)getData{

    NSString *urlStr = @"http://api2.pianke.me/ting/radio";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6"];

    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
        [_allListArr removeAllObjects];
        [_hotListArr removeAllObjects];
        [_carouselArr removeAllObjects];
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    dic = result;
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    data = [dic objectForKey:@"data"];
    NSMutableArray *allListArr = [NSMutableArray array];
    allListArr = [data objectForKey:@"alllist"];
    NSMutableArray *hotList = [NSMutableArray array];
    hotList = [data objectForKey:@"hotlist"];
    NSMutableArray *carouselArr = [NSMutableArray array];
    carouselArr = [data objectForKey:@"carousel"];
        NSLog(@"dic === %@",dic);
    for (NSMutableDictionary *dic in allListArr) {

        DHRadio *radio = [[DHRadio alloc]init];
        [radio setValuesForKeysWithDictionary:dic];
        [_allListArr addObject:radio];
    }
    for (NSMutableDictionary *dic in hotList) {
        DHRadio *radio = [[DHRadio alloc]init];
        [radio setValuesForKeysWithDictionary:dic];
        [_hotListArr addObject:radio];
    }
    for (NSMutableDictionary *dic in carouselArr) {
        DHCarousel *carousel = [[DHCarousel alloc]init];
        [carousel setValuesForKeysWithDictionary:dic];
        [_carouselArr addObject:carousel];
    }
//    NSLog(@"carousel.count == %ld",_carouselArr.count);
       
    [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
} failure:^(NSError *error) {
    
}];
}
#pragma mark- 上拉解析数据
- (void)getAFNetWorkingDataIsUpLoad{
    NSString *urlStr = @"http://api2.pianke.me/ting/radio_list";
    NSString *para = [NSString stringWithFormat:@"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&limit=9&start=%ld&version=3.0.6", self.nextPage];
    [GetData postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
       
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        for (NSMutableDictionary *dic in [bigDic objectForKey:@"list"]) {
            DHRadio *radio = [[DHRadio alloc]init];
            [radio setValuesForKeysWithDictionary:dic];
            [_allListArr addObject:radio];
        }
        [_myTableView.mj_footer endRefreshing];
        [_myTableView reloadData];
        
    } failure:^(NSError *error) {
        
        
    }];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
